export default {
  data() {
    return {
      ws: null, // WebSocket实例
    };
  },
  methods: {
    // 初始化WebSocket连接
    initWebSocket() {
      const wsUrl = 'ws://192.168.1.3:2346'; // 替换为你的WebSocket服务器URL
      this.ws = uni.connectSocket({
        url: wsUrl,
		success:()=>{
			
		}
      })
	 
   
      this.ws.onOpen(() => {
        console.log('WebSocket连接已打开');
      });
 
      this.ws.onMessage((message) => {
		  let data = JSON.parse(message.data)
        console.log('收到服务器消息：', message.data);
      });
 
      this.ws.onError((error) => {
        console.error('WebSocket连接发生错误', error);
      });
 
      this.ws.onClose(() => {
        console.log('WebSocket已关闭');
      });
    },
 
    // 发送消息
    sendMessage(data) {
      if (this.ws && data) {
        this.ws.send({
          data: JSON.stringify(data),
          success: () => {
            console.log('消息发送成功');
          },
          fail: () => {
            console.error('消息发送失败');
          },
        });
      }
    },
 
    // 关闭WebSocket连接
    closeWebSocket() {
      if (this.ws) {
        this.ws.close();
      }
    },
  },
 
};